var AlagisMap = function () {
    var ALAGISMAP = null,
        ICON_ARRAY = new Array(),
        popup = null,
        overlay = null,
        popup2 = null,
        overlay2 = null;
    var currentHotAreaId = null;

    function initAlagisHotArea(map) {
        alagis.source.EdushiMap.initHotArea({
            map: ALAGISMAP,
            http: "http://120.27.215.107:8081/hotArea",
            key: "",
            zoom: 14,
            zindex: 50,
            style: {
                fill: {
                    color: 'rgba(255,255,255,0.6)'
                },
                stroke: {
                    color: '#9B822D',
                    width: 2
                },
                text: {
                    font: '12px Calibri,sans-serif',
                    fill: {
                        color: '#000'
                    },
                    stroke: {
                        color: '#fff',
                        width: 3
                    }
                }
            }
        });
    }

    function initPointerMove() {
        ALAGISMAP.on('pointermove', function (evt) {
            if (evt.dragging) {
                return;
            }
            let pixel = evt.map.getEventPixel(evt.originalEvent);
            let hit = evt.map.hasFeatureAtPixel(pixel);
            if (hit) {
                evt.map.getTargetElement().style.cursor = 'pointer';
            } else {
                evt.map.getTargetElement().style.cursor = ' url(/public/images/openhand.cur) 8 8, default';
            }

        });

    }

    function addPopupOverlady() {
        popup = document.getElementById('popup');
        overlay = new alagis.Overlay(({
            element: popup,
            autoPan: true,
            autoPanAnimation: {
                duration: 250
            }
        }));
        ALAGISMAP.addOverlay(overlay);
        popup2 = document.getElementById('popup2');
        overlay2 = new alagis.Overlay(({
            element: popup2,
            autoPan: true,
            autoPanAnimation: {
                duration: 250
            }
        }));
        ALAGISMAP.addOverlay(overlay2);

    }

    function addHotAreaOverlay() {
        ALAGISMAP.on('click', function (evt) {
            let pixel = evt.map.getEventPixel(evt.originalEvent);
            let hit = evt.map.hasFeatureAtPixel(pixel);
            if (hit) {
                let feature = evt.map.forEachFeatureAtPixel(evt.pixel, function (feature) {
                    return feature;
                });
                if (feature) {
                    if(currentHotAreaId != feature.get("id")){
                        clearBaiduICON();
                    }
                    var coordinate = new Array();
                    coordinate.push(feature.get("centerx"));
                    coordinate.push(feature.get("centery"));
                    let baiDuPoint = alagisUtil.edushiToBaidu(coordinate);
                    let address = feature.get("address");
                    if (!address) {
                        address = feature.get("name");
                    }
                    popup.src = `/map/popup.htm?id=${feature.get("id")}&address=${address}&name=${feature.get("name")}&lng=${baiDuPoint.lng}&lat=${baiDuPoint.lat}&type=hot&tm=` + new Date().getTime();
                    popup.style.display = 'block';
                    currentHotAreaId = feature.get("id");
                    overlay.setPosition(coordinate);
                }
            }
        });
    }


    function addBaiDuDataIcon(data, pagenum) {
        ICON_ARRAY.forEach(x => {
            ALAGISMAP.removeOverlay(ALAGISMAP.getOverlayById(x));
        });
        ICON_ARRAY = [];
        data.forEach((obj, index) => {
            let iconNum = pagenum * 10 + index + 1;
            $("#Icon").append(`<div id="${obj.uid}" style="background-image:url(/public/images/dian.png);width:32px;height:33px; cursor:pointer;"
             onclick="AlagisMap.bdDataShowInfo('${obj.uid}','${obj.address}','${obj.name}','${obj.location.lng}','${obj.location.lat}')" title="${obj.name}">
             <span style="display:block;width:32px;line-height:33px;text-align:center;font-size:12px;color:#fff;font-weight:bold;margin-left:-4px;">${iconNum}</span></div>`);
            ICON_ARRAY.push(obj.uid);
            alagisUtil.addIconFeature(obj.uid, obj.location, ALAGISMAP);
        });
    }

    function baiDuIconDataShowInfo(uid, address, name, lngg, latt) {

        let point = {
            lng: lngg,
            lat: latt
        };
        let baiduPoint = alagisUtil.gcjToBaidDuPoint(point);
        popup2.src = `/map/popup.htm?id=${uid}&address=${address}&name=${name}&lng=${baiduPoint.lng}&lat=${baiduPoint.lat}&type=icon&tm=` + new Date().getTime();
        popup2.style.display = 'block';
        overlay2.setPosition(alagisUtil.gcjToEdushi(point));
    }

    function clearBaiduICON(){
        ICON_ARRAY.forEach(x => {
            ALAGISMAP.removeOverlay(ALAGISMAP.getOverlayById(x));
        });
        ICON_ARRAY = [];
        if(top.clearSearchCondition){
            top.clearSearchCondition();
        }
    }



    return {

        initAlagisMapEvent: function (map) {
            ALAGISMAP = map
            addPopupOverlady();
            addHotAreaOverlay();
            initAlagisHotArea();
            initPointerMove();
        },

        initBaiDuDataIcon: function (data, pagenum) {
            addBaiDuDataIcon(data, pagenum);
        },
        bdDataShowInfo: function (uid, address, name, lng, lat) {
            baiDuIconDataShowInfo(uid, address, name, lng, lat);
        }
    }
}();